// import { Session, pomelo } from "pomelo2-creator";
import EventBase from "../eventBase";
import DataMgr from "./dataManager";
import NativeMgr from "./nativeManager";
import NetMgr from "./netManager";







export default class ServerMgr extends EventBase {
    private static instance: ServerMgr;


    percent: number = 0.6;//服务器收益
    // pomelo:Session
    private userInfo: any = {

    }


    public static Ins() {
        if (!this.instance) {
            this.instance = new ServerMgr();

            this.instance.init();
            // this.instance.servo = new httpFunc.request('http://118.31.115.133:8086', {});
        }
        return this.instance;


    }
    init() {
        const list = DataMgr.Ins().getLocalStorage('adList');
        this.userInfo.list = list || [];
    }

    async post(url: string, data: any) {
        switch (url) {
            case 'watchAds':
                return await this.watchAds(data);
            case 'getUserInfo':
                return await this.getUserInfo()
                break;
        }
    }


    async watchAds(data) {
        //     Event.obj().Emit("getEcpm",
        //    0     type+   
        //    1     "_"+ item.getEcpm() +
        //    2     "_" + item.getSdkName() +
        //    3     "_" + item.getCustomSdkName() +
        //    4     "_" + item.getSlotId() +
        //    5     "_" + item.getReqBiddingType() +
        //    6     "_" + item.getErrorMsg() +
        //    7     "_" + item.getRequestId() +
        //    8     "_" + item.getRitType() +
        //    9     "_" + item.getAbTestId() +
        //    10     "_" + item.getScenarioId() +
        //    11     "_" + item.getSegmentId() +
        //    12     "_" + item.getChannel() +
        //    13     "_" + item.getSubChannel() +
        //    14     "_" + item.getCustomData()
        // );


        //EcpmInfo: 
        // SdkName: gdt,
        // CustomSdkName: null,
        // SlotId: 5101028619962596,
        // Ecpm: 559.0,
        // ReqBiddingType: 1,
        // ErrorMsg: null,
        // RequestId: 2owbtjtyxbhsc01,
        // RitType: banner,
        // AbTestId: ,
        // ScenarioId: ,
        // SegmentId: 0,
        // Channel: ,
        // SubChannel: ,
        // customData: {}


        const infoStr = data.split('_');


        const info: any = {
            RitType: infoStr[8],//广告类型
            SdkName: infoStr[2],//渠道
            Ecpm: infoStr[1],//价格
            SlotId: infoStr[4],//广告位
            RequestId: infoStr[7],//请求码
            time: Date.now(),//时间

        }
        const diffecpm = await this.httpRequest({ event: 'finish', source: NativeMgr.Ins().channel(), worth: info.ecpm, ...info });


        //换算成金币
        // info.Ecpm = diffecpm;

        console.log("info === ", info);

        this.userInfo.list.unshift(info);



        this.userInfo.list = this.userInfo.list.slice(0, 100);
        console.log('server userInfo = ', this.userInfo);
        DataMgr.Ins().setLocalStorage('adList', this.userInfo.list);


        return this.userInfo;
    }


    getUserInfo() {
        return this.userInfo;
    }




    async httpRequest(options) {

        const deviceId = NativeMgr.Ins().deviceId();
        const appId = NativeMgr.Ins().appId();


        options.appid = appId;
        options.unionid = deviceId;
        options.worth = Number(options.worth) > 0 ? Number(options.worth) : 0

        const info = await NetMgr.Ins().servo.post('/payment/advertising/transaction', options, true);

        if (info.status === 200) {
            const redPack = Number(info.body.ecpm);
            const diff = redPack - DataMgr.Ins().userInfo.redPack;
            DataMgr.Ins().userInfo.redPack = redPack;
            console.log('ecpm = ', DataMgr.Ins().userInfo.redPack);
            return diff
        }

    }

}


